<div class="nav-sub w1180">
	<ul class="zoom">
		<li><a class="on" >新增销售商品</a></li>
		<li><a href="/crm/org/list">商户列表</a></li>
		<li><a href="/crm/org/goods-list">商品列表</a></li>
	</ul>
</div>
	<div class="main-ct w1180">
<!-- 		<a href="javascript:history.go(-1);" class="btn-cs btn-blue btn-fillet mt10">返 回</a> -->

<!-- 		<h2 class="tit-border tit-border-orange">新增销售商品</h2> -->

		<div class="form-info mt20">
		<dl>
			<dt>销售日期：</dt>
			<dd>
<!-- 		 onchange="window.location.reload(true)" 	  onclick="setCookie('select_1',this.selectedIndex)" -->
			<select name="sell_data" id="sell-data" onclick="setCookie('select_1',this.selectedIndex)" onchange="window.location.reload(true)">
<!-- 				<option value="">请选择</option> -->
				<?php if ($this->select_week):?>
    			<?php foreach ($this->select_week as $k=>$v):?>
    				<option value="<?php echo $k;?>"><?php echo $v;?></option>
				<?php endforeach; ?>
    			<?php endif;?>
				
			</select>
			</dd>
		</dl>
		
	</div>
	<p class="mt30 txt-center">
	</p>
		
		<div class="add-tab-list zoom">
			<div class="fl tab-l">
				<select multiple class="form-control" size="28" id="menuList">
				<?php if ($this->goods):?>
    			<?php foreach ($this->goods as $k=>$v):?>
        			<option value="<?php echo $v['product_id']; ?>"><?php echo $v['product_name']; ?></option>
        		<?php endforeach; ?>
    			<?php endif;?>
					
				</select>
			</div>
			<div class="tab-r">
				<div class="add-tab-l">
					<div class="c-btn"><a class="btn-cs btn-fillet btn-green btn-add-menu" data-add-type="1">添加至早餐</a></div>
					<div class="add-wrap" goods-list="1" id="select-goods-one">
					</div>
				</div>

				<div class="add-tab-l">
					<div class="c-btn"><a class="btn-cs btn-fillet btn-green btn-add-menu" data-add-type="2">添加至中餐</a></div>
					<div class="add-wrap" goods-list="2" id="select-goods-two"></div>
				</div>

				<div class="add-tab-l">
					<div class="c-btn"><a class="btn-cs btn-fillet btn-green btn-add-menu" data-add-type="3">添加至晚餐</a></div>
					<div class="add-wrap" goods-list="3"  id="select-goods-three"></div>
				</div>
			</div>
		</div>

	</div>

<script>
// 	var se;
//     se = document.getElementById('sell-data');
//     se.onchange = function(){
//         localStorage.value = this.value;
//         localStorage.index = this.selectedIndex;
//         console.log( localStorage.index +";"+ localStorage.value );
//     }
//     window.onload = function(){
//         alert( localStorage.index +";"+ localStorage.value );
//         se.options[ localStorage.index ].selected = true;
//     }

    function setCookie(name, value) {
        var exp = new Date();
        exp.setTime(exp.getTime() + 24 * 60 * 60 * 1000);
        //document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
        document.cookie = name + "=" + escape(value);
//暂去除 
//         var sellData = $('#sell-data').val().toString();
// 		if(sellData != ''){
// 			ajaxSelectGoodsByType(sellData);
// 		}
		
    }
    
    function getCookie(name)
    {
        var regExp = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        var arr = document.cookie.match(regExp);
        if (arr == null) {
            return null;
        }
        return unescape(arr[2]);
    }

    var selectedIndex = getCookie("select_1");
    if(selectedIndex != null) {
        document.getElementById("sell-data").selectedIndex = selectedIndex;
    }


	//加载默认 选中  该时间内的 商品
    $(document).ready(function(){
    	//出售日期
		var sellData = $('#sell-data').val().toString();
		if(sellData != ''){
			var orgId = "<?php echo (int) $_GET['org_id'];?>";
			ajaxSelectGoodsByType(sellData, orgId);
		}
		
    	
    });

    function ajaxSelectGoodsByType(sellData, orgId){
		//通过指定日期 区分不同类型时间段  显示不同区域

    	//提交数据
		$.post("/crm/org/ajax-select-sell-goods",{sell_data:sellData, org_id:orgId}, function(result){

			console.log(result);
		    if(result.result){
				if(result.html1){
					$('#select-goods-one').append(result.html1);
				}
				if(result.html2){
					$('#select-goods-two').append(result.html2);
				}
				if(result.html3){
					$('#select-goods-three').append(result.html3);
				}

			}
		  },'json');
		

		
    }
    
$(function(){
	
	$('.btn-add-menu').each(function(index, element){

		//$('body').on('click' , this , function(){
		$('.btn-add-menu').click(function(event) {

	
			//出售日期
			var sellData = $('#sell-data').val().toString();
			if(sellData == ''){
				alert('请选择出售日期');
				return false;
			}
			
			var goodsName = $("#menuList").find("option:selected").text();
			var goodsId = $('#menuList').val().toString();

            var success = true;
        	$("div[goods-list='"+$(this).attr("data-add-type")+"']").find("a").each(function(){
        
            	if($(this).attr("data-goods-id") == parseInt(goodsId)){
            			success = false;
            			return false;
            	}
        
        	});

			if(goodsId != '' && goodsName != '' && success == true){

				var addType = $(this).data('add-type');
				if(addType == ''){
					alert('类型有误');
					return false;
				}

				$(this).parents('.c-btn').next('.add-wrap').append('<div class="tab-item" data-selectd-goods-id="' + goodsId + '">' + goodsName + '<a href="javascript:;" class="btn-del" data-goods-type="' + addType + '" data-goods-id="' + goodsId + '">X</a></div>');

				
			//提交数据
			$.post("/crm/org/ajax-sell-goods",{'add_type':addType,'goods_id':goodsId,'sell_data':sellData},function(result){
			    if(result.result){
			    	//alert(result.reason);
				}
			  },'json');
				
			}
		

		})
	})

	
// 	$('.tab-item .btn-del').click(function(){
// 		$(this).parents('.tab-item').remove();
// 	});

    $('body').on('click' , '.tab-item .btn-del' , function(){

    	var goodsId = $(this).data('goods-id');
    	//alert(goodsId);

    	//早 中 晚
		var addType = $(this).data('goods-type');

		//alert(addType);
		if(addType == ''){
			alert('类型有误');
			return false;
		}

		//出售日期
		var sellData = $('#sell-data').val().toString();
		if(sellData == ''){
			alert('请选择出售日期');
			return false;
		}

    	//提交数据
		$.post("/crm/org/ajax-del-sell-goods",{'add_type':addType,'goods_id':goodsId,'sell_data':sellData},function(result){
		    if(result.result){
		    	 //alert(result.reason);
			}
		  },'json');

		  
    	$(this).parents('.tab-item').remove();
    }); 
        
})
</script>